<h1 id="异步编程">异步编程</h1>
<p>Quick的异步是用Go的协程实现的  </p>
<p>Quick的异步编程通过调用内部函数<code>async(func)</code>，并传入一个需要异步执行的函数即可。  </p>
<h3 id="示例一：异步执行一个函数">示例一：异步执行一个函数</h3>
<pre><code class="hljs language-javascript">start = <span class="hljs-title function_">timestamp</span>()

<span class="hljs-title function_">async</span>(${
    <span class="hljs-title function_">println</span>(<span class="hljs-string">&quot;it&#x27;s in async&quot;</span>)
    <span class="hljs-title function_">sleep</span>(<span class="hljs-number">1500</span>)
})

<span class="hljs-title function_">println</span>(<span class="hljs-string">&quot;cost: &quot;</span>, <span class="hljs-title function_">timestamp</span>() - start, <span class="hljs-string">&quot;ms&quot;</span>)
</code></pre>
<p>输出</p>
<pre><code>cost:  0 ms
it&#x27;s in async
</code></pre>
<p>我们可以发现用时统计是最先输出的，并且用时为0ms，说明的确是异步的</p>
<h3 id="示例二：-异步执行一个函数并获取其返回值">示例二： 异步执行一个函数并获取其返回值</h3>
<p><code>async()</code>被调用后会返回一个函数，执行这个函数，即可获取异步返回值，但同时会造成阻塞，
直到子协程执行结束。<br>我们先不调用<code>async()</code>返回的函数，看看运行结果</p>
<pre><code class="hljs language-javascript">start = <span class="hljs-title function_">timestamp</span>()

res = <span class="hljs-title function_">async</span>(${
    <span class="hljs-title function_">println</span>(<span class="hljs-string">&quot;it&#x27;s in async&quot;</span>)
    <span class="hljs-title function_">sleep</span>(<span class="hljs-number">1500</span>)
    <span class="hljs-keyword">return</span> <span class="hljs-number">998</span>
})

<span class="hljs-comment">// println(&quot;async result:&quot;, res())</span>

<span class="hljs-title function_">println</span>(<span class="hljs-string">&quot;cost: &quot;</span>, <span class="hljs-title function_">timestamp</span>() - start, <span class="hljs-string">&quot;ms&quot;</span>)
</code></pre>
<p>输出</p>
<pre><code>cost:  0 ms
it&#x27;s in async
</code></pre>
<p>我们发现和示例一的结果是一样的，然后我们再调用<code>async()</code>返回的函数，看看运行结果</p>
<pre><code class="hljs language-javascript">start = <span class="hljs-title function_">timestamp</span>()

res = <span class="hljs-title function_">async</span>(${
    <span class="hljs-title function_">println</span>(<span class="hljs-string">&quot;it&#x27;s in async&quot;</span>)
    <span class="hljs-title function_">sleep</span>(<span class="hljs-number">1500</span>)
    <span class="hljs-keyword">return</span> <span class="hljs-number">998</span>
})

<span class="hljs-title function_">println</span>(<span class="hljs-string">&quot;async result:&quot;</span>, <span class="hljs-title function_">res</span>())

<span class="hljs-title function_">println</span>(<span class="hljs-string">&quot;cost: &quot;</span>, <span class="hljs-title function_">timestamp</span>() - start, <span class="hljs-string">&quot;ms&quot;</span>)
</code></pre>
<p>输出</p>
<pre><code>it&#x27;s in async
async result: 998
cost:  1501 ms
</code></pre>
<p>结果是: 用时统计是最后执行的，并且用时1.5s</p>
<h3 id="跨协程传值">跨协程传值</h3>
<p>可以解决一些多协程带来的问题</p>
<ol>
<li><p>方法1：传参</p>
<pre><code class="hljs language-javascript"><span class="hljs-keyword">for</span> i=<span class="hljs-number">0</span>; i&lt;<span class="hljs-number">100</span>; i++ {
 <span class="hljs-title function_">async</span>($ num {
     <span class="hljs-title function_">println</span>(num, i)
 }, i)
}
</code></pre>
</li>
<li><p>方法2：通过阻塞队列</p>
<pre><code class="hljs language-javascript">queue = <span class="hljs-title function_">blockList</span>()
<span class="hljs-keyword">for</span> i=<span class="hljs-number">0</span>; i&lt;<span class="hljs-number">100</span>; i++ {
 queue.<span class="hljs-title function_">set</span>(i)
 <span class="hljs-title function_">async</span>(${
     num = queue.<span class="hljs-title function_">get</span>()
     <span class="hljs-title function_">printf</span>(<span class="hljs-string">&quot;num:%v ; i:%v \n&quot;</span>, num, i)
 })
}
</code></pre>
</li>
</ol>
